package com.wyb.service.impl;

import com.wyb.dao.GoodsDao;
import com.wyb.dao.impl.GoodsDaoImpl;
import com.wyb.entity.Goods;
import com.wyb.service.GoodsService;
import com.wyb.util.JDBCUtils;

import java.sql.Connection;
import java.util.Collections;
import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao gd=new GoodsDaoImpl();
    @Override
    public List<Goods> queryAllGoods() {
        return gd.selectAllGoods();
    }

    @Override
    public boolean addGoods(Goods goods) {
        boolean boo=false;
        Connection conn=null;
        try {
            conn= JDBCUtils.startTransaction();
            if(gd.insertGoods(goods)==1){
                boo=true;
            }
            JDBCUtils.commit(conn);
        } catch (Exception e) {
            JDBCUtils.rollback(conn);
            throw new RuntimeException(e);
        }
        return boo;
    }

    @Override
    public Goods queryGoodsById(int id) {
        return gd.selectGoodsById(id);
    }

    @Override
    public boolean deleteGoodsById(int id) {
        boolean boo=false;
        Connection conn=null;
        try {
            conn= JDBCUtils.startTransaction();
            if(gd.deleteGoodsById(id)==1){
                boo=true;
            }
            JDBCUtils.commit(conn);
        } catch (Exception e) {
            JDBCUtils.rollback(conn);
            throw new RuntimeException(e);
        }
        return boo;
    }

    @Override
    public boolean updateGoods(Goods goods) {
        boolean boo=false;
        Connection conn=null;
        try {
            conn= JDBCUtils.startTransaction();
            if(gd.updateGoodsById(goods)==1){
                boo=true;
            }
            JDBCUtils.commit(conn);
        } catch (Exception e) {
            JDBCUtils.rollback(conn);
            throw new RuntimeException(e);
        }
        return boo;
    }

    @Override
    public boolean updateGoodsStatus(int status, int id) {
        boolean boo=false;
        Connection conn=null;
        try {
            conn= JDBCUtils.startTransaction();
            if(gd.updateGoodsStatusById(status,id)==1){
                boo=true;
            }
            JDBCUtils.commit(conn);
        } catch (Exception e) {
            JDBCUtils.rollback(conn);
            throw new RuntimeException(e);
        }
        return boo;
    }

    @Override
    public List<Goods> queryGoodsByGoodsName(String goodsName) {
        return gd.selectGoodsByGoodsName(goodsName);
    }
}
